/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GastoSelect.java
 *
 * Created on 15/07/2011, 11:20:04 PM
 */

package TransportesMarroquin;

import com.mysql.jdbc.Connection;

import java.awt.event.ActionEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Owner
 */
public class GastoSelect extends javax.swing.JFrame {

    /** Creates new form GastoSelect */
    int num;
    public GastoSelect(int cod) {
        initComponents();

        num=cod;
        cv.setText(""+num);




        try {

                // coneccion a la base de datos
        MysqlDataSource datasource= new MysqlDataSource();
        datasource.setUser("root");
        datasource.setPassword("");
        datasource.setDatabaseName("practicasp8");
        datasource.setServerName("localhost");

          Connection conexion = (Connection) datasource.getConnection();


        Statement s = (Statement) conexion.createStatement();

 // query database
        ResultSet resultSet = s.executeQuery( "SELECT * FROM gasto where idgasto="+num);
        ResultSetMetaData metaData = (ResultSetMetaData) resultSet.getMetaData();
         int numerodecolumnas = metaData.getColumnCount();

       while (resultSet.next()){

            for(int i=1;i<=numerodecolumnas;i++){
                 switch(i){
                     // creo este switch par delimitar el nombre de los label ya establecidos :D
                     case 2:
                         cv.setText(""+resultSet.getObject(i));
                       break;
                     case 5:
                         cgas.setText(""+resultSet.getObject(i));
                       break;
                     case 6:
                         cace.setText(""+resultSet.getObject(i));
                       break;
                     case 7:
                         cfre.setText(""+resultSet.getObject(i));
                       break;
                     case 8:
                         cotr.setText(""+resultSet.getObject(i));
                       break;
                     case 9:
                         ctot.setText(""+resultSet.getObject(i));
                       break;
                     case 4:
                         cdes.setText(""+resultSet.getObject(i));
                       break;
                     case 10:
                         cfec.setText(""+resultSet.getObject(i));
                       break;    


             }

         } // fin del for
       } // fin del while

        } catch (SQLException ex) {
            Logger.getLogger(ClienteSelect.class.getName()).log(Level.SEVERE, null, ex);
    /** Creates new form FurgonesSelect */
        }
    }

    public GastoSelect() {
        initComponents();
        throw new UnsupportedOperationException("Not yet implemented");
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bcan = new javax.swing.JButton();
        cgas = new javax.swing.JTextField();
        cace = new javax.swing.JTextField();
        aceite = new javax.swing.JRadioButton();
        gas = new javax.swing.JRadioButton();
        frenos = new javax.swing.JRadioButton();
        cfre = new javax.swing.JTextField();
        cotr = new javax.swing.JTextField();
        otros = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ctot = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cv = new javax.swing.JTextField();
        cfec = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        cdes = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        codigop = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bcan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/regresar.png"))); // NOI18N
        bcan.setText("Regresar");
        bcan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bcanActionPerformed(evt);
            }
        });

        cgas.setEnabled(false);
        cgas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cgasActionPerformed(evt);
            }
        });
        cgas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cgasKeyTyped(evt);
            }
        });

        cace.setEnabled(false);

        aceite.setText("Aceite");
        aceite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceiteActionPerformed(evt);
            }
        });

        gas.setText("Gas");
        gas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gasActionPerformed(evt);
            }
        });

        frenos.setText("Frenos");
        frenos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                frenosActionPerformed(evt);
            }
        });

        cfre.setEnabled(false);

        cotr.setEnabled(false);

        otros.setText("Otros");
        otros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                otrosActionPerformed(evt);
            }
        });

        jLabel3.setText("Descripción");

        jLabel2.setText("Total");

        ctot.setEnabled(false);

        jLabel4.setText("Codigo Vehiculo");

        cv.setEnabled(false);
        cv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cvActionPerformed(evt);
            }
        });

        cfec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cfecActionPerformed(evt);
            }
        });

        jLabel15.setText("Fecha");

        cdes.setColumns(20);
        cdes.setLineWrap(true);
        cdes.setRows(5);
        cdes.setEnabled(false);
        jScrollPane2.setViewportView(cdes);

        jPanel1.setBackground(new java.awt.Color(143, 0, 0));

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel13.setForeground(new java.awt.Color(255, 255, 255));
        jLabel13.setText("Piloto");

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel14.setForeground(new java.awt.Color(255, 255, 255));
        jLabel14.setText("Codigo");

        codigop.setFont(new java.awt.Font("Tahoma", 1, 18));
        codigop.setForeground(new java.awt.Color(255, 255, 255));
        codigop.setText("#####");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 228, Short.MAX_VALUE)
                .addComponent(jLabel14)
                .addGap(18, 18, 18)
                .addComponent(codigop)
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(codigop)
                    .addComponent(jLabel14))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bcan)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(gas)
                                    .addComponent(cgas, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(aceite)
                                    .addComponent(cace, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cfre, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(frenos))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cotr, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(otros))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(ctot, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(30, 30, 30))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(110, 110, 110)
                                .addComponent(cv, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(98, 98, 98)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                        .addComponent(jLabel15)
                        .addGap(33, 33, 33)
                        .addComponent(cfec, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cace, cfre, cgas, cotr, ctot});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cfec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(aceite)
                                .addGap(15, 15, 15)
                                .addComponent(cace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(frenos)
                                    .addComponent(otros)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addComponent(cfre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(gas)
                                .addGap(15, 15, 15)
                                .addComponent(cgas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cotr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(ctot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(bcan, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cace, cfre, cgas, cotr, ctot});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bcanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bcanActionPerformed
        dispose();   
        Gasto gas;
            gas = new Gasto("Select * from gasto");
             gas.setVisible(true);
             gas.setBounds(114, 107, Toolkit.getDefaultToolkit().getScreenSize().width-114, Toolkit.getDefaultToolkit().getScreenSize().height-107);   


    }//GEN-LAST:event_bcanActionPerformed

    private void otrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_otrosActionPerformed
       cotr.setEditable(true);
}//GEN-LAST:event_otrosActionPerformed

    private void gasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gasActionPerformed
     cgas.setEnabled(true);
     
}//GEN-LAST:event_gasActionPerformed

    private void aceiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceiteActionPerformed
      cace.setEnabled(true);
}//GEN-LAST:event_aceiteActionPerformed

    private void cgasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cgasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cgasActionPerformed

    private void cvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cvActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cvActionPerformed

    private void cfecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cfecActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cfecActionPerformed

    private void cgasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cgasKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_cgasKeyTyped

    private void frenosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_frenosActionPerformed
       cfre.setEditable(true);
    }//GEN-LAST:event_frenosActionPerformed

    /**
    * @param args the command line arguments
    */

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GastoSelect().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton aceite;
    private javax.swing.JButton bcan;
    private javax.swing.JTextField cace;
    private javax.swing.JTextArea cdes;
    private javax.swing.JTextField cfec;
    private javax.swing.JTextField cfre;
    private javax.swing.JTextField cgas;
    private javax.swing.JLabel codigop;
    private javax.swing.JTextField cotr;
    private javax.swing.JTextField ctot;
    private javax.swing.JTextField cv;
    private javax.swing.JRadioButton frenos;
    private javax.swing.JRadioButton gas;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton otros;
    // End of variables declaration//GEN-END:variables

}
